/**
 * 	    \mainpage
 * 		interpolate_regions.cpp
 *
 * 		\section sec_copy Copyright
 *
 * 		Copyright 2011 Stefan Fruhner <stefan@itp.tu-berlin.de>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
#include <mylibs/CardiacMesh.hpp>
#include <mylibs/myinifiles.hpp>
#include <mylibs/mystring.hpp>
#include <mylibs/units.hpp>
#include <mylibs/lists.h>
#include <mylibs/myio.hpp>
#include <iostream>
#include <fstream>
#include <sys/stat.h> 	// mkdir

using namespace std;
using namespace mylibs;

#ifdef DEBUG
	#define print(x) (cout << __FILE__ << ":" << __FUNCTION__ <<"() (" <<__LINE__ << ") : " << x << endl)
#else
	#define print(x) (cout << x << endl)
#endif

// global variables
string write_basename = "";
mystring input  = mystring();
mystring output = mystring("pacing_nodes/pacing.nodes.vtx");
mystring unit 	 = mystring("um");
mystring unit_output 	 = mystring("cm");

bool verbose 		 = false;
bool create_list 	 = false;
int  left_ventricle  = 12; // inner of the left ventricle
int  right_ventricle = 13; // inner of the right ventricle
double fraction 	 = 0.3;
int strategy 		 = 0;
int strategy_ref	 = 0;
string method 		 = "pearls";

int main(int argc, char** argv){

	myIniFiles ini(argc, argv, verbose);

	ini.set_info(toString(argv[0]) + "\n\
	(compiled on " + toString(__DATE__) + " " + toString(__TIME__) + ")\n\n\
");
	ini.register_param("input"	 ,"i", "The mesh that contains regions and fiber directions.");
	ini.register_flag("list"	 ,NULL ,"List of possible regions");
	ini.register_param("unit"	 ,"u" ,"Unit of length within the mesh.");
	ini.register_param("unit_output" ,"o" ,"Unit of length in the output.");
	ini.register_flag("verbose" ,"v", "Additionally messages on stdout.");

	ini.check();

	verbose 			= ini.exists("verbose");
	input				= ini.read("input", input);
	unit				= ini.read("unit", unit);
	unit_output			= ini.read("unit_output", unit_output);
	create_list		 	= ini.exists("list");

//	------------------------- Error checking -----------------------------------
	if (input.empty()) cmdline::exit("-i [--input] : No input mesh ... exit.");

	// if no extension supplied, the assume *.pts
	if (input.file_ext() == mystring()) input.file_new_ext(".pts");
	// check input mesh
	if (not input.file_exists()) {
		mystring err = input + " does not exist.";
		cmdline::exit(err);
	}


//	-------------------- Error checking finished -------------------------------
	// talk about parameters
	if (verbose){
		cmdline::section("Parameters");
		TELL(input);
		TELL(create_list)
	}

	// load the input mesh
	CardiacMesh mesh(input, verbose);
	double factor = units::SI_Prefix_Factor(unit, unit_output);
	cout << "- Bounding Box :" << endl;
	BoundingBox bb = mesh.bounding_box();
	cout << bb << endl;

	double w = bb.width() / factor;
	double d = bb.depth() / factor;
	double h = bb.height() / factor;

	cout << " - width  = " << w  << " " << unit_output << endl;
	cout << " - depth  = " << d  << " " << unit_output << endl;
	cout << " - height = " << h << " "  << unit_output << endl;
	cout << " - volume = " << w*d*h << " " <<unit_output << "^3" << endl;
	cout << " - Center of mass : "
		 << mesh.center_of_mass() / factor << " " << unit_output << endl;


	if (create_list){
		if (verbose) cmdline::msg(" - Scanning list of regions");
		myList<int> regions = mesh.scan_for_regions();
		if (verbose) cout << "   ";
		for (myList<int>::iterator i = regions.begin(); i != regions.end(); i++){
			cout << *i << " ";
		}
		cout << "\n\n" << endl;

		exit(0);
	}

	mesh.region_statistics(unit, unit_output);

	return 0;
}
